﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Microsoft.SharePoint;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class TopMap : UserControl
    {
        private string AbsolutePath = string.Empty;
        private string AbsoluteUri = string.Empty;
        private string RawUrl = string.Empty;
        private string SiteTitle = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //相对路径 eg. /companyinfo/default.aspx
            AbsolutePath = this.Page.Request.Url.AbsolutePath.ToLower();
            //绝对URL eg. http://terry:9002/companyinfo/default.aspx
            AbsoluteUri = this.Page.Request.Url.AbsoluteUri.ToLower();
            //原始URL
            RawUrl = this.Page.Request.RawUrl.ToLower();
            string strUrl="";
            string childtitle = "";
            string childWebName = "";
            string childWebTitle = "";

            if (!IsPostBack)
            {
                if (RawUrl.StartsWith("/companyinfo"))
                {
                    strUrl = "/companyinfo/default.aspx";
                    SiteTitle = "公司信息";
                }
                else if (RawUrl.StartsWith("/projectinfo"))
                {
                    strUrl = "/projectinfo/default.aspx";
                    SiteTitle = "项目信息";
                }
                else if (RawUrl.StartsWith("/infomationcenter"))
                {
                    strUrl = "/infomationcenter/default.aspx";
                    SiteTitle = "资讯中心";
                }
                else if (RawUrl.StartsWith("/enterpriseculture"))
                {
                    strUrl = "/enterpriseculture/default.aspx";
                    SiteTitle = "企业文化";
                }
                else if (RawUrl.StartsWith("/knowledgecenter"))
                {
                    strUrl = "/knowledgecenter/default.aspx";
                    SiteTitle = "知识中心";
                }
                else if (RawUrl.StartsWith("/_layouts/sp_xhweb/employeedetails.aspx"))
                {
                    SiteTitle = "公司信息";
                }
                else if (RawUrl.StartsWith("/_layouts/sp_xhweb/dispformitem.aspx"))
                {
                    string weburl = Request["web_url"].ToString();
                    if (!string.IsNullOrEmpty(weburl))
                    {
                        if (weburl.StartsWith("/companyinfo"))
                        {
                            SiteTitle = "公司信息";
                        }
                        else if (weburl.StartsWith("/projectinfo"))
                        {
                            SiteTitle = "项目信息";
                        }
                        else if (weburl.StartsWith("/infomationcenter"))
                        {
                            SiteTitle = "资讯中心";
                        }
                        else if (weburl.StartsWith("/enterpriseculture"))
                        {
                            SiteTitle = "企业文化";
                        }
                        else if (weburl.StartsWith("/knowledgecenter"))
                        {
                            SiteTitle = "知识中心";
                        }
                        else
                        {
                            SiteTitle = "星浩资本";
                        }
                    }
                }
                else
                {
                    SiteTitle = "星浩资本";
                }


                if (!string.IsNullOrEmpty(this.Request.Url.ToString()))
                {
                    using (SPSite site = new SPSite(this.Request.Url.ToString()))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            //判断是否为子项目信息
                            if (web.ParentWeb != null &&!string.IsNullOrEmpty(web.ParentWeb.Name) && web.ParentWeb.Name.ToLower() == "projectinfo")
                            {
                                childWebName = web.Name;
                                childWebTitle = web.Title;
                            }

                            SPList list = web.Lists["SiteMapList"];
                            if (list != null && list.ItemCount != 0)
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["UrlAddress"] != null && !string.IsNullOrEmpty(item["UrlAddress"].ToString()))
                                    {
                                        if (AbsolutePath.StartsWith(item["UrlAddress"].ToString()))
                                        {
                                            childtitle = item.Title;

                                            if(!string.IsNullOrEmpty(item["ParentID"].ToString())&&item["ParentID"].ToString()!="0")
                                            {
                                                foreach (SPListItem childitem in list.Items)
                                                {
                                                    if (!string.IsNullOrEmpty(childitem["MapID"].ToString()) && item["ParentID"].ToString() == childitem["MapID"].ToString())
                                                    {
                                                        childtitle = childitem.Title;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                StringBuilder str = new StringBuilder();
                str.Append("<a href='/default.aspx'>");
                str.Append("星浩门户网站");
                str.Append("</a>");

                if (childtitle == "")
                {
                    str.Append(" " + SiteTitle);
                }
                else
                {
                    str.AppendFormat("<a href='{0}'> {1}</a>", strUrl, SiteTitle);
                    if (!string.IsNullOrEmpty(childWebName) && !string.IsNullOrEmpty(childWebTitle))
                    {
                        str.AppendFormat("<a href='{0}'> {1}</a>", "/projectinfo/" + childWebName + "/default.aspx", childWebTitle);
                    }
                    
                    str.Append(" "+childtitle);
                }

                lblSiteTitle.InnerText = SiteTitle;
                lblMapHtml.InnerHtml = str.ToString();
            }
        }
    }
}
